package com.hnu.highlynewapp.app;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnu.highlynewapp.entity.PolicyEntity;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.utils.ValidationUtils;
import com.hnu.highlynewapp.vo.PublishActivityVo;
import com.hnu.highlynewapp.vo.PublishPolicyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hnu.highlynewapp.entity.ActivityEntity;
import com.hnu.highlynewapp.service.ActivityService;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 活动
 *
 * @author hnu_worker
 * @email 825963704@qq.com
 * @date 2021-01-04 22:01:47
 */
@Api(tags = "活动api管理")
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 发布活动
     */
    @ApiOperation("发布活动")
    @PostMapping("/publish")
    public R publish(@Validated @RequestBody PublishActivityVo vo, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidationUtils.getErrors(result);
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data",map);
        } else {
            activityService.publish(vo);
            return R.ok();
        }
    }

    /**
     * 获取所有活动（时间降序）
     * @return
     */
    @ApiOperation("获取所有活动")
    @GetMapping("/getlist")
    public R getList() {
        List<ActivityEntity> list = activityService.list(new QueryWrapper<ActivityEntity>().orderByDesc("publish_time"));
        return R.ok().put("data", list);
    }

    /**
     * 根据id获取活动
     * @return
     */
    @ApiOperation("根据id获取活动")
    @GetMapping("/get/{activityId}")
    public R getById(@PathVariable("activityId") Long activityId) {
        ActivityEntity activityEntity = activityService.getById(activityId);
        if (activityEntity == null) {
            return R.error(BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getCode(), BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getMessage());
        }
        return R.ok().put("data", activityEntity);
    }

    /**
     * 根据角色id获取自己发布活动
     * @return
     */
    @ApiOperation("根据角色id获取自己发布活动")
    @GetMapping("/getlist/{roleId}")
    public R getListByRoleId(@ApiParam(value = "角色id",required = true) @PathVariable("roleId") Long roleId,
                             @ApiParam(value = "角色类型", required = true) @RequestParam Integer type) {
        List<ActivityEntity> list = activityService.list(new QueryWrapper<ActivityEntity>().eq("publisher_id",roleId).eq("publisher_type",type).orderByDesc("publish_time"));
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    //@RequiresPermissions("highlynewapp:activity:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = activityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiIgnore
    @RequestMapping("/info/{activityId}")
    //@RequiresPermissions("highlynewapp:activity:info")
    public R info(@PathVariable("activityId") Long activityId){
		ActivityEntity activity = activityService.getById(activityId);

        return R.ok().put("activity", activity);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @RequestMapping("/save")
    //@RequiresPermissions("highlynewapp:activity:save")
    public R save(@RequestBody ActivityEntity activity){
		activityService.save(activity);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    //@RequiresPermissions("highlynewapp:activity:update")
    public R update(@RequestBody ActivityEntity activity){
		activityService.updateById(activity);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    //@RequiresPermissions("highlynewapp:activity:delete")
    public R delete(@RequestBody Long[] activityIds){
		activityService.removeByIds(Arrays.asList(activityIds));

        return R.ok();
    }

}
